﻿
@{
    ViewData["Title"] = "添加文章";
    var cate = ViewData["cate"] as List<article_category>;
    var slist = cate.Select(x => new SelectListItem()
    {
        Text = x.is_show ? x.category_name : $"{x.category_name}-不显示",
        Value = x.id.ToString()
    }).ToList();
    var selected = slist.FirstOrDefault();
    if (selected != null)
    {
        selected.Selected = true;
    }
}

<div class="row">
    <form id="ajaxForm" enctype="multipart/form-data" class="form-horizontal">
        <div class="container-fluid">
            <div class="row form-group">
                <label class="control-label col-md-2">分类</label>
                <div class="col-md-10">
                    <select class="form-control" asp-items="slist" id="cateid" name="cateid"></select>
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">标题</label>
                <div class="col-md-10">
                    <input class="form-control" name="title" id="title" type="text" required/>
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">内容</label>
                <div class="col-md-10">
                    <div id="editor">
                        <p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p>
                    </div>
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">关键字</label>
                <div class="col-md-10">
                    <input class="form-control" name="keywords" id="keywords" type="text" />
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">是否置顶</label>
                <div class="col-md-10">
                    <input class="i-checks" name="top" id="top" type="checkbox" value="true" />
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">排序数字</label>
                <div class="col-md-10">
                    <input class="form-control" name="sort_num" id="sort_num" type="number" max="100" min="1" value="100" />
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">是否显示</label>
                <div class="col-md-10">
                    <input class="i-checks" name="is_show" id="is_show" type="checkbox" value="true" />
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">封面图片</label>
                <div class="col-md-10">
                    <input class="form-control" name="img" id="img" type="file" />
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-6 col-md-offset-2">
                    <button type="button" class="btn btn-primary" onclick="SaveArticleUrl()">保存</button>
                </div>
            </div>
        </div>
    </form>
</div>

@section scripts{

    <script type="text/javascript">
        var E = window.wangEditor
        var editor = new E('#editor')
        // 或者 var editor = new E( document.getElementById('editor') )
        // editor.customConfig.uploadImgShowBase64 = true
        // 将图片大小限制为 3M
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
        editor.customConfig.uploadImgServer = '/upload/image'
        // 限制一次最多上传 5 张图片
        editor.customConfig.uploadImgMaxLength = 5
        editor.customConfig.uploadFileName = 'image'
        editor.create();

        E.fullscreen.init(editor);
        E.viewSource.init(editor);

        function SaveArticleUrl() {
            if ($("#title").val().length === 0) {
                utils.ShowError("请输入标题");
                return;
            }
            if ($("#keywords").val().length === 0) {
                utils.ShowError("请输入关键字");
                return;
            }
            if (editor.txt.text().length === 0) {
                utils.ShowError("请输入文章内容");
                return;
            }

            $("#ajaxForm").ajaxSubmit({
                type: 'post',
                url: '@Url.Action("Save")',
                data: {
                    content: editor.txt.html()
                },  // data是作为额外提交的表单数据
                success: function (e) {
                    if (e.msg == 'ok') {
                        utils.ShowSuccess(e.data, function () {
                            location.href = '@Url.Action("List")';
                        });
                    }
                },
                error: function (e) {
                    utils.ShowError(e.data);
                }
            });
        }
    </script>
}

